package ch.sv7.tool.dbmanager.db;

import java.util.StringTokenizer;

public abstract class AbstractDatabaseSchemaFactory implements DatabaseSchemaFactory {

	public DatabaseSchema getDatabaseSchema() {
		DatabaseSchema schema =  getDatabaseSchema((String)null);
		return schema;
	}
	
	public DatabaseSchema getDatabaseSchema(String[] tables) {
		if (tables == null || tables.length == 0) {
			tables = getTables();
		}
		DatabaseSchema schema = new DatabaseSchema();
		for (int i = 0; i < tables.length; i++) {
			String tableName = tables[i];
			TableSchema ts = getTableSchema(tableName);
			if (ts != null)
				schema.addTableSchema(ts);
		}

		schema.buildReferences();
		return schema;
	}

	public DatabaseSchema getDatabaseSchema(String tables) {
		if (tables == null || tables.trim().length() == 0) {
			return getDatabaseSchema(getTables());
		}
		StringTokenizer tokens = new StringTokenizer(tables, ",");
		DatabaseSchema schema = new DatabaseSchema();
		while (tokens.hasMoreTokens()) {
			String tableName = tokens.nextToken().trim();
			TableSchema ts = getTableSchema(tableName);
			if (ts != null)
				schema.addTableSchema(ts);
		}
		schema.buildReferences();
		return schema;
	}

	public abstract TableSchema getTableSchema(String s);
	
	public abstract String[] getTables();
}
